<?php

namespace App\Http\Controllers;

use App\Jobs\Queue;
use App\Models\Consumptions;
use App\Models\GrGood;
use App\Models\GrOrder;
use App\Models\GrSeckill;
use App\service\JwtService;
use App\service\Particle;
use App\service\RabbitmqServer;
use Illuminate\Cache\RedisLock;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class Seckill extends Controller
{
    //用户秒杀
    public function userSeckill(Request $request)
    {
        $seckill_id = $request->post('seckill_id');
        $is_start = Redis::get('seckill_time' . $seckill_id);
        $token = $request->header('token');
        $user = JwtService::decodeToken($token);//解析token获取用户id
        $user_id = $user->data->id;
        $number = Redis::llen('number' . $seckill_id);
        if ($number <= 0) {
            return ['code' => 500, 'msg' => '库存不足', 'data' => []];
        }
        if ($is_start != null) {
            $start = json_decode($is_start, true);
            if (time() >= $start['start_time']) { //判断是否开始
                if (time() < $start['end_time']) { //判断是否结束
                    $number = Redis::get('purchase_number' . $user_id . '_' . $seckill_id);
                    $data = GrSeckill::where('seckill_id', $seckill_id)->pluck('participate_frequency');//取出限制次数
                    $limit_number = $data[0];
                    if ($number <= $limit_number) {//判断购买次数
                        $user = JwtService::decodeToken($token);//解析token获取用户id
                        $user_id = $user->data->id;
                        $seckill = GrSeckill::where('seckill_id', $seckill_id)->first();
                        $goods_data = GrGood::where('goods_id', $seckill->goods_id)->first();
                        $goods_money = $goods_data->goods_now_price;
                        $address_id = $request->post('address_id');//收货地址
                        $particle = Particle::generateParticle(); //雪花算法
                        $goods_number = $request->post('goods_number');
                        $order_data = [ //订单数据
                            'order_sn' => $particle,
                            'user_id' => $user_id,
                            'goods_money' => $goods_money * $goods_number,
                            'total_money' => ($seckill->seckill_price) * $goods_number,
                            'address_id' => $address_id
                        ];
                        //商品数据
                        $goods_data = [
                            'goods_id' => $seckill->goods_id,
                            'goods_number' => $goods_number,
                        ];
                        $end_time = $seckill->seckill_end_time; //结束时间

                        return $this->dispatch(new Queue($goods_data, $order_data, $end_time, $seckill_id));//rabbitmq队列调用传参
                    }
                    return ['code' => 500, 'msg' => '本秒杀每人每天限购' . $limit_number, 'data' => []];
                }
                return ['code' => 500, 'msg' => '活动已经结束', 'data' => []];
            }
            return ['code' => 500, 'msg' => '还未开始', 'data' => []];
        }

    }

    /**
     * 秒杀队列
     * @param $seckill_id
     * @param $goods_stock
     * @param $start_time
     * @param $end_time
     */
    public function seckillPreheat($seckill_id, $goods_stock, $start_time, $end_time)
    {
        if ($seckill_id != null) {
            for ($i = 0; $i < $goods_stock; $i++) {
                //库存进入redis队列 进行库存的预热
                Redis::lpush('number' . $seckill_id, 1);
            }
            Redis::setex('seckill_time' . $seckill_id, strtotime($end_time) - time(), json_encode(['start_time' => strtotime($start_time), 'end_time' => strtotime($end_time)]));
        }
    }

//    public function index(Request $request)
//    {
//
//        $token = $request->header('token');
//        $seckill_id = $request->post('seckill_id');
//        $user = JwtService::decodeToken($token);//解析token获取用户id
//        $user_id = $user->data->id;
//        $seckill = GrSeckill::where('seckill_id', $seckill_id)->first();
//        $goods_data = GrGood::where('goods_id', $seckill->goods_id)->first();
//        $goods_money = $goods_data->goods_now_price;
//        $address_id = $request->post('address_id');
//        $particle = Particle::generateParticle();
//        $goods_number = $request->post('goods_number');
//        $order_data = [
//            'order_sn' => $particle,
//            'user_id' => $user_id,
//            'goods_money' => $goods_money * $goods_number,
//            'total_money' => ($seckill->seckill_price) * $goods_number,
//            'address_id' => $address_id
//        ];
//        $order_data = GrOrder::create($order_data);
//
//
////        $this->dispatch(new ProcessPodcast($order_data, 10));
//    }
    /**
     * 超时审核
     * @param $id
     */
    public function orderUpdate($id)
    {
        Log::info("开始对订单".$id.'进行支付状态审核');
        $data = GrOrder::where('id', $id)->first('is_pay');
        //取出订单数据进行验证订单支付状态如果已支付不更改否则进行修改为取消订单状态
//        Log::info($data[0]);
        if ($data->is_pay == 0) {
            GrOrder::where('id', $id)->update(['order_status' => 3]);
        }
    }
}
